'''子弹类'''

import pygame
import game_utils
from pygame.sprite import Sprite

class Bullet(Sprite):
    '''子弹类'''
    def __init__(self,screen,ship,speed:float=0.8,color:str='#00ff00',width=3,height=15):
        super(Bullet,self).__init__()
        self.screen = screen
        self.ship = ship
        self.bullet_w = width
        self.bullet_h = height
        self.speed = speed
        self.color = game_utils.color_to_rgb(color)
        
        #在屏幕的 0,0 位置创建一个子弹
        self.rect = pygame.Rect(0,0,self.bullet_w,self.bullet_h)
        self.rect.centerx = self.ship.rect.centerx
        self.rect.top = self.ship.rect.top

        #储存子弹的位置为小数
        self.y = float(self.rect.y)

    def update(self):
        self.y -= self.speed
        self.rect.centery = self.y
    
    def draw_bullet(self):
        '''把子弹绘制到屏幕'''        
        pygame.draw.rect(self.screen,self.color,self.rect) #绘制一个子弹形状到屏幕 注意查看绘图API的调用